package com.ykq.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykq.common.entity.AclRole;
import com.ykq.common.entity.AclUserRole;
import com.ykq.common.vo.Result;
import com.ykq.system.dao.AclRoleDao;
import com.ykq.system.dao.AclUserRoleDao;
import com.ykq.system.service.AclRoleService;
import com.ykq.system.vo.FenRoleVo;
import com.ykq.system.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: qy156-education-cloud
 * @description:
 * @author: 闫克起2
 * @create: 2022-12-05 14:47
 **/
@Service
public class AclRoleServiceImpl implements AclRoleService {
    @Autowired
    private AclRoleDao roleDao;
    @Autowired
    private AclUserRoleDao userRoleDao;


    @Override
    public Result findRoleByUserId(Long userid) {
        //1. 查询所有的角色
        List<AclRole> aclRoles = roleDao.selectList(null);
        //2. 查询当前用户具有的角色
        List<AclRole> userRoles = roleDao.selectByUserid(userid);
        List<String> userRoleIds = userRoles.stream().map(item -> item.getId()).collect(Collectors.toList());
        Map<String,Object> map=new HashMap<>();
        map.put("aclRoles",aclRoles);
        map.put("userRoles",userRoleIds);
        return new Result(2000,"查询成功",map);
    }

    @Override
    @Transactional
    public Result fenUserRole(FenRoleVo fenRoleVo) {
        //删除当前用户具的角色
        UpdateWrapper<AclUserRole> wrapper=new UpdateWrapper<>();
        wrapper.eq("user_id",fenRoleVo.getUserId());
        userRoleDao.delete(wrapper);
        //添加该用户具有的新角色 insert into 表名 values(值,值...)(值,值...)(值,值....)  动态sql语句
        List<AclUserRole> list=new ArrayList<>();
        for(String roleId:fenRoleVo.getRoleIds()){
            AclUserRole aclUserRole = new AclUserRole();
            aclUserRole.setIsDeleted(0);
            aclUserRole.setRoleId(roleId);
            aclUserRole.setUserId(fenRoleVo.getUserId());
            aclUserRole.setGmtCreate(LocalDateTime.now());
            aclUserRole.setGmtModified(LocalDateTime.now());
            list.add(aclUserRole);
        }
        userRoleDao.batchInsert(list);

        return new Result(2000,"分配角色成功");
    }

    @Override
    public Result findByConditionPage(RoleVo roleVo, Integer current, Integer pageSize) {
        IPage<AclRole> page=new Page<>(current,pageSize);
        QueryWrapper<AclRole> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(roleVo.getRoleName())){
            wrapper.like("role_name",roleVo.getRoleName());
        }
        if(roleVo.getIsDeleted()!=null){
             wrapper.eq("is_deleted",roleVo.getIsDeleted());
        }

        IPage<AclRole> page1 = roleDao.selectPage(page, wrapper);
        return new Result(2000,"分页查询角色成功",page1);
    }
}
